<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/10
 * Time: 10:08
 */
namespace app\admin\controller;

use app\admin\model\AuthRule;
use think\Db;

class Admin extends Base{

    /**
     * 管理员列表
     */
    public function admin_list(){
        //获取管理员列表数据
        $admin_list = Db::table('yf_admin')->alias('a')
            ->field('a.id,a.admin_username,a.admin_realname,a.admin_email,a.admin_tel,a.admin_addtime,a.admin_last_time,a.admin_open,c.title')
            ->join('yf_auth_group_access b','a.id=b.uid','left')
            ->join('yf_auth_group c','b.group_id=c.id','left')
            ->order('a.id asc')
            ->paginate(10);
        // 获取分页显示
        $page = $admin_list->render();
        $admin_list = $admin_list->toArray();
        // 模板变量赋值
        $this->assign('list', $admin_list['data']);
        $this->assign('page', $page);
        $this->assign('total', $admin_list['total']);
        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 管理员添加
     */
    public function admin_add(){
        if ($this->request->isPost()){
            $admin= model('Admin');
            //调用模型添加管理员 用户名，密码，邮箱，电话，是否开启，真实姓名，所属角色
            $admin_id = $admin->admin_add(input('admin_username'),input('admin_pwd'),input('admin_email',''),input('admin_tel',''),input('admin_open',0),input('admin_realname',''),input('group_id'));
            if($admin_id){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>0,'msg'=>$admin->geterror()]);
            }
        }else{
            $auth_group=Db::name('auth_group')->where('status',1)->select();
            $this->assign('auth_group',$auth_group);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 管理员编辑
     */
    public function admin_edit(){
        if ($this->request->isPost()){
            $admin= model('Admin');
            $data=input('post.');
            $admin_id = $admin->edit($data);
            if($admin_id){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'修改失败']);
            }
        }else{
            $admin_id = input('param.id','');
            if (empty($admin_id)){
                $this->error('管理员id不能为空');
            }
            $auth_group=Db::name('auth_group')->where('status',1)->select();//用户组
            $admin_info = Db::table('yf_admin')->alias('a')
                ->field('a.*,b.group_id')
                ->join('yf_auth_group_access b','a.id=b.uid','left')
                ->where('a.id',$admin_id)
                ->find();
            $this->assign('admin_info',$admin_info);
            $this->assign('auth_group_access',$admin_info['group_id']);
            $this->assign('auth_group',$auth_group);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 管理员删除
     */
    public function admin_del()
    {
        $admin_id=input('id');
        if (empty($admin_id)){
            return json(['code'=>0,'msg'=>'管理员ID不存在']);
        }
        Db::name('admin')->delete($admin_id);
        $rst=Db::name('auth_group_access')->where('uid',$admin_id)->delete();
        if($rst!==false){
            return json(['code'=>200,'msg'=>'管理员删除成功']);
        }else{
            return json(['code'=>200,'msg'=>'管理员删除失败']);
        }
    }

    /**
     * 更改管理员状态
     */
//    public function change_admin()
//    {
//        if (request()->isAjax()){
//            $admin_id = input('param.admin_id',0);
//            if ($admin_id == 0 || $admin_id == 1){
//                return json(['code'=>0,'msg'=>'管理员传入有误']);
//            }else{
//                $admin_open = db('admin')->where('id',$admin_id)->value('admin_open');
//                if ($admin_open == 0){
//                    db('admin')->where('id',$admin_id)->setField(['admin_open'=>1]);
//                }else{
//                    db('admin')->where('id',$admin_id)->setField(['admin_open'=>0]);
//                }
//            }
//            return json(['code'=>200,'msg'=>'管理员状态设置成功']);
//        }
//    }

    /**
     * 管理员分组列表
     */
    public function admin_group_list(){
        $data=Db::table('yf_auth_group')->paginate(20);
        // 获取分页显示
        $page = $data->render();
        $list_info = $data->toArray();
        // 模板变量赋值
        $this->assign('list', $list_info['data']);
        $this->assign('total', $list_info['total']);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 用户组添加
     */
    public function admin_group_add(){
        if ($this->request->isPost()){
            $new_rules = input('new_rules/a');
            $imp_rules = implode(',', $new_rules);
            $data=array(
                'title'=>input('title'),
                'rules'=> $imp_rules,
                'status'=>input('status',0),
                'describe'=>input('describe',''),
                'add_time'=>time(),
            );
            $rst=Db::name('auth_group')->insert($data);
            if($rst!==false){
                return json(['code'=>200,'msg'=>'用户组添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'用户组添加失败']);
            }
        }else{
            $data=AuthRule::get_ruels_tree();
            $this->assign('data',$data);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 用户组编辑
     */
    public function admin_group_edit(){
        if ($this->request->isPost()){
            $new_rules = input('new_rules/a');
            $imp_rules = implode(',', $new_rules);
            $data=array(
                'id'=>input('id'),
                'title'=>input('title'),
                'rules'=> $imp_rules,
                'status'=>input('status',0),
                'describe'=>input('describe',''),
            );
            $rst = Db::name('auth_group')->update($data);
            if($rst!==false){
                return json(['code'=>200,'msg'=>'用户组添加成功']);
            }else{
                return json(['code'=>0,'msg'=>'用户组添加失败']);
            }
        }else{
            $group=Db::name('auth_group')->find(input('id'));
            $data=AuthRule::get_ruels_tree();
            $this->assign('admin_group',$group);
            $this->assign('data',$data);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 用户组删除
     */
    public function admin_group_del()
    {
        $group_id=input('id');
        if (empty($group_id)){
            return json(['code'=>0,'msg'=>'用户组ID不存在']);
        }
        $num = Db::name('auth_group_access')->where('group_id',$group_id)->count();
        if ($num >0){
            return json(['code'=>0,'msg'=>'用户组删除失败,还有管理员属于这个用户组，请先释放再删除']);
        }else{
            $rst = Db::name('auth_group')->where('id',$group_id)->delete();
            if ($rst){
                return json(['code'=>200,'msg'=>'用户组删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'用户组删除失败']);
            }
        }
    }

    /**
     * 空操作
     */
    public function _empty(){
        if($this->check_admin_login()){
            $this->redirect('admin/Index/index');
        }else{
            $this->redirect('admin/login');
        }
    }

}